var setting = {
	data : {
		key:{
			name:"permissionName"
		},
		simpleData : {
			enable : true,
			idKey:"id",
			pIdKey:"parentId"
		}
	},
	check:{
		enable:true
	},
	async: {
		enable: true,
		otherParam: {"roleId":roleId},
		url:"admin/org/permission/getAllPermissions"
	},
	callback: {
		onClick: nodeClick
	}
};

var myTree;
$(document).ready(function() {
	myTree = $.fn.zTree.init($("#permissionTree"), setting);
	//确定按钮事件
	$("#permissionBtn").click(function(){
		var nodes = myTree.getCheckedNodes(true);
		submitPermission(nodes);
	});
});
//提交权限信息
function submitPermission(nodes){
	
	var pers=new Array();
	$.each(nodes,function(i,v){
		pers.push(v.id);
	});
	
	$.post("admin/org/permission/bindRolePermissions",{
		jsonData:JSON.stringify({
			roleId:roleId,
			permissions:pers
		})
	},function(data){
		if(data.msg=="success"){
			layer.alert("分配成功",function(){
				layer.close(layer.index);
			});
		}else{
			layer.alert("未知错误",function(){
				layer.close(layer.index);
			});
		}
	},"json");
}

//节点点击事件,用来控制点击节点名称勾选复选框
function nodeClick(event, treeId, treeNode){
	if(treeNode.checked){
		myTree.checkNode(treeNode, false, true);
	}else{
		myTree.checkNode(treeNode, true, true);
	}
}